package com.zhibang.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhibang.dto.AccountxtzhDTO;
import com.zhibang.entity.Account;
import com.zhibang.entity.AccountRole;
import com.zhibang.entity.AccountShop;
import com.zhibang.mapper.AccountRoleMapper;
import com.zhibang.mapper.AccountShopMapper;
import com.zhibang.service.AccountService;
import com.zhibang.mapper.AccountMapper;
import com.zhibang.util.SHAUtil;
import com.zhibang.vo.AccountAddVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author lewin
* @description 针对表【account(账号表)】的数据库操作Service实现
* @createDate 2025-03-05 09:18:16
*/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
    implements AccountService{
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private AccountRoleMapper accountRoleMapper;
    @Resource
    private AccountShopMapper accountShopMapper;

    @Override
    public IPage<AccountxtzhDTO> selectAccountInfo(AccountxtzhDTO query, IPage page) {
        return accountMapper.selectAccountInfo(query,page);
    }
    @Override
    public boolean updatePasswordById(Integer id, String newPassword) {
        Account account = new Account();
        account.setId(id);
        account.setPassword(newPassword);
        return this.updateById(account);
    }


    @Override
    public int addAccount(AccountAddVO accountVo) {
        SHAUtil shaUtil = new SHAUtil();
        String salt = shaUtil.getSalt();
        String password = shaUtil.sha256Pwd("123", salt);
        accountVo.setPassword(password);
        accountVo.setSalt(salt);
        Account a = getAccount(accountVo);
        accountMapper.insert(a);
        //插入角色表
        Account account = accountMapper.getUsers(a.getAccount());
        AccountRole r = new AccountRole();
        r.setAid(account.getId());
        r.setRid(accountVo.getRid());
        accountRoleMapper.insert(r);
        AccountShop s = new AccountShop();
        s.setAid(account.getId());
        s.setSid(accountVo.getSid());
//        accountShopMapper.insert(s);
        //插入数据库
        return accountShopMapper.insert(s);
//        return 0;
    }

    @Override
    public int updateAccount(AccountAddVO accountVo) {
        Account a = getAccount(accountVo);
        System.out.println(a.getId());
        //更新门店表
        int sid = accountShopMapper.selectByAid(a.getId());
        AccountShop s = new AccountShop();
        s.setId(sid);
        s.setAid(a.getId());
        s.setSid(accountVo.getSid());
        accountShopMapper.updateById(s);
        //更新角色表
        int rid = accountRoleMapper.getRoleIdByAid(a.getId());
        AccountRole r = new AccountRole();
        r.setId(rid);
        r.setAid(a.getId());
        r.setRid(accountVo.getRid());
        accountRoleMapper.updateById(r);
        return accountMapper.updateAccount(a);
    }

    @Override
    public Account getAccountById(Integer id) {
        return accountMapper.getAccountById(id);
    }
    private static Account getAccount(AccountAddVO accountVo) {
        Account accountEntity = new Account();
        accountEntity.setId(accountVo.getId());
        accountEntity.setAccount(accountVo.getAccount());
        accountEntity.setPassword(accountVo.getPassword());
        accountEntity.setSalt(accountVo.getSalt());
        accountEntity.setName(accountVo.getName());
        accountEntity.setPhone(accountVo.getPhone());
        accountEntity.setState(accountVo.getState());
        accountEntity.setCreateTime(accountVo.getCreateTime());
        accountEntity.setIsDelete(accountVo.getIsDelete());
        accountEntity.setEmail(accountVo.getEmail());
        return accountEntity;
    }

    @Override
    public boolean updateStateById(Integer id, Integer state) {
        Account account = new Account();
        account.setId(id);
        account.setState(state);
        return accountMapper.updateById(account) > 0;
    }


}




